const mysql = require('mysql');
const db = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: '1998',
  database: 'demo'
})
//测试连接
/* db.query('select 1', (err, res) => {
  if (err) {
    console.log(err);
  } else {
    console.log(res);
  }
}) */

//查询数据
// const sqlString = 'select  *  from student where id>4'
// const sqlString = 'select  *  from student where id>?'

//插入数据  ?表示占位符

// const sqlString = 'insert into student  set ?'  简化
// const data = { name: 'tan11', number: 123 }
// const sqlString = 'insert into student(number,name) values (?,?)'
/* db.query(sqlString, [data.number, data.name], (err, res) => {
  if (err) {
    console.log(err);
  } else if (res.affectedRows == 1) {
    console.log(res);
    console.log('插入成功');
  }
}) */
/* 简化  db.query(sqlString, data, (err, res) => {
  if (err) {
    console.log(err);
  } else if (res.affectedRows == 1) {
    console.log(res);
    console.log('插入成功');
  }
})  */

//更新数据
// const data = { name: 'tan12', number: 123, id: 2 }
/* const sqlString = 'update student set name=?,number=? where id=?'
db.query(sqlString, [data.name, data.number, data.id], (err, res) => {
  if (err) {
    console.log(err);
  } else if (res.affectedRows == 1) {
    console.log(res);
    console.log('更新成功');
  }
}) */
//简化
/* const sqlString = 'update student set ? where id=?'
db.query(sqlString, [data, data.id], (err, res) => {
  if (err) {
    console.log(err);
  } else if (res.affectedRows == 1) {
    console.log(res);
    console.log('更新成功');
  }
}) */

// 删除数据
const sqlString = 'delete from student where id=?'
db.query(sqlString, 1, (err, res) => {
  if (err) {
    console.log(err);
  } else if (res.affectedRows == 1) {
    console.log(res);
    console.log('删除成功');
  }
})

// 标记删除，数据库里面有个状态字段，用update将状态改变，回显删除